package com.ccrfid.rmc6.service.sys;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.sys.KeyValueEntity;
import com.ccrfid.rmc6.pojo.dto.OutlineDto;
import com.ccrfid.rmc6.repository.sys.KeyValueRepository;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Service
@Transactional
public class KeyValueServiceImpl implements KeyValueService {

    public static final String HandsetAppVersionKey = "handset-app-version";
    public static final String HandsetAppFileNameKey = "handset-app-file-name";

    @Value("${file.upload.folder}")
    private String fileUploadFolder;
    @Autowired
    private KeyValueRepository keyValueRepository;

    @Override
    public OutlineDto getOutline(Long id) {
        String key = Long.toString(id);
        KeyValueEntity entity = keyValueRepository.getByK(key);
        if (entity != null) {
            OutlineDto dto = new OutlineDto(id, entity.getV());
            return dto;
        } else {
            return null;
        }
    }

    @Override
    public void saveOutline(OutlineDto dto) {
        KeyValueEntity entity = new KeyValueEntity(Long.toString(dto.getId()), dto.getContent());
        keyValueRepository.save(entity);
    }

    @Override
    public KeyValueEntity getKeyValue(String key) {
        return keyValueRepository.getByK(key);
    }

    @Override
    public void saveKeyValue(KeyValueEntity entity) {
        keyValueRepository.save(entity);
    }

    @Override
    public String getHandsetAppVersion() {
        KeyValueEntity entity = getKeyValue(HandsetAppVersionKey);
        if (entity != null) {
            return entity.getV();
        } else {
            return "";
        }
    }

    @Override
    public void uploadHandsetAppFile(String version, String fileName, InputStream inputStream) throws IOException {
        File file = new File(fileUploadFolder + '/' + fileName);
        File parent = file.getParentFile();
        //创建目录
        if (!parent.exists()) {
            parent.mkdirs();
        }
        //保存文件
        FileUtils.copyInputStreamToFile(inputStream, file);
        //更新数据库记录
        saveKeyValue(new KeyValueEntity(HandsetAppVersionKey, version));
        saveKeyValue(new KeyValueEntity(HandsetAppFileNameKey, fileName));
    }

    @Override
    public File downloadHandsetAppFile() throws AppException {
        KeyValueEntity entity = getKeyValue(HandsetAppFileNameKey);
        if (entity == null) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        File file = new File(fileUploadFolder + '/' + entity.getV());
        if (!file.exists()) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        return file;
    }
}
